/*
 * @Descripttion:
 * @version:
 * @Author: lehaixia
 * @Date: 2021-07-21 14:00:24
 * @LastEditors: lehaixia
 * @LastEditTime: 2022-03-03 10:03:51
 */
import request from '@/utils/request'
export const appCode = 'call-center' // cc字典
export const domainCode = 'marketing' // cc字典
const baseURL = process.env.VUE_APP_LOGIN_BASE_API

/**
 * 根据字典类型查询字典
 * @param {string} dictTypeCode 数据字典编码
 * @param {string} appCode 应用编码
 * @param {string} domainCode 业务域编码
 * */
export function getDictByType({ dictTypeCode, appCode, domainCode }) {
  const key = `${dictTypeCode}_${appCode}_${domainCode}`
  let data = sessionStorage.getItem(key)
  if (data) {
    try {
      data = JSON.parse(data)
      return Promise.resolve({
        data
      })
    } catch (error) {
      console.log(error)
    }
  }
  return request({
    url: `/data-dict/api/v2/dictionary/${dictTypeCode}`,
    baseURL,
    method: 'get',
    params: { appCode, domainCode }
  }).then(res => {
    res.data = res.data.sort((a, b) => { return a.sort - b.sort })
    try {
      sessionStorage.setItem(key, JSON.stringify(res.data))
    } catch (error) {
      console.log(error)
    }
    return res
  })
}
/** 获取cc呼叫中心字典
 * @parmas {string} dictTypeCode 数据字典编码
 */
export function ccDict(dictTypeCode, isCrm = false) {
  return getDictByType({
    dictTypeCode,
    appCode: isCrm ? 'top2-crm' : appCode,
    domainCode
  })
}

/** 获取字典对应名称 */
export function getDicteName(dictTypeCode, v) {
  v = v !== undefined ? v : ''
  return getDictByType({
    dictTypeCode,
    appCode,
    domainCode
  }).then(async res => {
    for (const d of res.data) {
      if (d.dictCode.toString() === v.toString()) {
        return d.dictName
      }
    }
  })
}

/**
 * 根据字典类型查询字典
 * @param {string} dictTypeCode 数据字典编码
 * @param {string} appCode 应用编码
 * @param {string} domainCode 业务域编码
 * @returns {Array} 数据字典值列表
 * */
export const getDictList = async({ dictTypeCode, appCode, domainCode }) => {
  const res = await request({
    url: `/data-dict/api/v2/dictionary/${dictTypeCode}`,
    method: 'get',
    params: { appCode, domainCode }
  })
  return res && res.data
}

/**
 * 根据字典类型批量查询字典值列表
 * @param {Array} dictTypeCodes 数据字典编码
 * @returns {Array} 数据字典值列表
 * */
export const getDictByPatchType = async(appCode, dictTypeCodes) => {
  const res = await request({
    url: `/data-dict/api/v2/dictionary/patch/dictTypeCode`,
    method: 'post',
    params: { appCode, domainCode },
    data: dictTypeCodes
  })
  return res && res.data
}

/**
 * 根据父级ID获取字典
 * @param {*} parentDictId
 * @returns
 */
export function getDataDictByParentId(parentDictId) {
  return request({
    url: `/data-dict/api/v2/dictionary/child?parentDictId=${parentDictId}`,
    method: 'GET'
  })
}
/**
 * mdm档案
 * @param {*} custDocCode
 * @returns
 */
export function custDocDef(custDocCode) {
  const data = new FormData()
  data.append('pageFlag', false)
  data.append('filtersRaw', JSON.stringify([{ id: 'custDocCode', property: 'custDocCode', value: custDocCode, operator: '=' }]))
  return request({
    url: '/select-service/api/v1/custDocDef/page',
    method: 'POST',
    data
  }).then(res => {
    return res.data.records || []
  })
}
